/***************************************************************
 *  Copyright (c) 2007 by GroupMe! Team (www.groupme.net)
 *  All rights reserved
 *
 *  This file is part of the GroupMe! Project. You can utilize
 *  software of the GroupMe! project under the terms of the 
 *  GNU Lesser General Public License as published by the
 *  Free Software Foundation in version 2.1.
 * 
 *  The GNU Lesser General Public License can be found at:
 *  http://www.gnu.org/licenses/lgpl.html
 *
 *  This copyright notice MUST APPEAR in all copies of the file!
 ***************************************************************/
package net.groupme.model.dao;

import java.net.URI;
import java.net.URISyntaxException;
import java.sql.Types;

import javax.sql.DataSource;

import net.groupme.exceptions.InsertFailedException;
import net.groupme.exceptions.UpdateFailedException;
import net.groupme.utils.sql.GetStringFromTableWhereIDQuery;

import org.apache.log4j.Logger;
import org.springframework.jdbc.core.SqlParameter;
import org.springframework.jdbc.object.SqlUpdate;
import org.springframework.jdbc.support.GeneratedKeyHolder;

/**
 * 
 * This class provides... This interface defines...
 * 
 * @author Mischa Frank <a
 *         href="mailto:code@planetfrank.net">code@planetfrank.net</a>
 * 
 * @version 1.0, created on 24.05.2007
 * 
 */
public class StyleDAOImpl implements StyleDAO {

	// table
	private static final String TABLE_SIMPLEGROUPSTYLE = DBProperties
			.getString("GroupmeDB.TABLE_SIMPLEGROUPSTYLE"); //$NON-NLS-1$

	// columns
	private static final String SIMPLEGROUPSTYLE_ID = DBProperties
			.getString("GroupmeDB.SIMPLEGROUPSTYLE_ID"); //$NON-NLS-1$

	private static final String SIMPLEGROUPSTYLE_NAME = DBProperties
			.getString("GroupmeDB.SIMPLEGROUPSTYLE_NAME"); //$NON-NLS-1$

	private static final String SIMPLEGROUPSTYLE_BACKGROUNDIMAGE = DBProperties
			.getString("GroupmeDB.SIMPLEGROUPSTYLE_BACKGROUNDIMAGE"); //$NON-NLS-1$

	private static final String SIMPLEGROUPSTYLE_BACKGROUNDCOLOR = DBProperties
			.getString("GroupmeDB.SIMPLEGROUPSTYLE_BACKGROUNDCOLOR"); //$NON-NLS-1$

	@SuppressWarnings("unused")
	private static final Logger log = Logger.getLogger(StyleDAOImpl.class);

	private DataSource ds;

	private String id;

	public StyleDAOImpl(DataSource ds, String id) {
		this.ds = ds;
		this.id = id;
	}

	public StyleDAOImpl(DataSource ds, String name, String backgroundColor,
			String backgroundImage) throws InsertFailedException {
		this.ds = ds;
		SqlUpdate su = new SqlUpdate(ds,
				"INSERT INTO " + TABLE_SIMPLEGROUPSTYLE //$NON-NLS-1$
						+ " (" + SIMPLEGROUPSTYLE_BACKGROUNDCOLOR + ", "
						+ SIMPLEGROUPSTYLE_BACKGROUNDIMAGE + ") VALUES (?, ?)");
		su.declareParameter(new SqlParameter("backgroundColor", Types.VARCHAR)); //$NON-NLS-1$
		su.declareParameter(new SqlParameter("backgroundImage", Types.VARCHAR)); //$NON-NLS-1$
		su.compile();
		Object[] oa = new Object[2];
		oa[0] = new String(backgroundColor);
		oa[1] = new Integer(backgroundImage);
		GeneratedKeyHolder keyHolder = new GeneratedKeyHolder();
		int count = su.update(oa, keyHolder);
		if (count == 0)
			throw new InsertFailedException();
		this.id = keyHolder.getKey().toString();
	}
	
	/* (non-Javadoc)
	 * @see net.groupme.model.Identifiable#getId()
	 */
	public String getId() {
		return this.id;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see net.groupme.model.Identifiable#getDescription()
	 */
	public String getDescription() {
		return this.getLabel() + " Backgroundimage: "
				+ this.getBackgroundImage() + " Backgroundcolor: "
				+ this.getBackgrundColor();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see net.groupme.model.Identifiable#getLabel()
	 */
	public String getLabel() {
		return this.getName();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see net.groupme.model.Identifiable#getURI()
	 */
	public URI getURI() {
		try {
			return new URI("http://www.groupme.net/simplegroupstyle/" + id); //$NON-NLS-1$
		} catch (URISyntaxException e) {
			e.printStackTrace();
			return null;
		} 
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see net.groupme.model.Identifiable#setDescription(java.lang.String)
	 */
	public void setDescription(String description) throws UpdateFailedException {
		// no need to be implemented
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see net.groupme.model.Identifiable#setLabel(java.lang.String)
	 */
	public void setLabel(String label) {
		// no need to be implemented
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see net.groupme.model.Style#getBackgroundImage()
	 */
	public String getBackgroundImage() {
		GetStringFromTableWhereIDQuery getBackgroundImageQuery = new GetStringFromTableWhereIDQuery(
				ds, SIMPLEGROUPSTYLE_BACKGROUNDIMAGE, TABLE_SIMPLEGROUPSTYLE,
				id);
		return (String) getBackgroundImageQuery.execute().iterator().next();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see net.groupme.model.Style#getBackgrundColor()
	 */
	public String getBackgrundColor() {
		GetStringFromTableWhereIDQuery getBackgroundColorQuery = new GetStringFromTableWhereIDQuery(
				ds, SIMPLEGROUPSTYLE_BACKGROUNDCOLOR, TABLE_SIMPLEGROUPSTYLE,
				id);
		return (String) getBackgroundColorQuery.execute().iterator().next();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see net.groupme.model.Style#setBackgroundColor(java.lang.String)
	 */
	public void setBackgroundColor(String color) throws UpdateFailedException {
		SqlUpdate su = new SqlUpdate(
				ds,
				"UPDATE " + TABLE_SIMPLEGROUPSTYLE + " SET " + SIMPLEGROUPSTYLE_BACKGROUNDCOLOR + " = ? WHERE " + SIMPLEGROUPSTYLE_ID + " = ?"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
		su.declareParameter(new SqlParameter("color", Types.VARCHAR)); //$NON-NLS-1$
		su.declareParameter(new SqlParameter("ID", Types.INTEGER)); //$NON-NLS-1$
		su.compile();
		Object[] oa = new Object[2];
		oa[0] = color;
		oa[1] = new Integer(id);
		int count = su.update(oa);
		if (count == 0)
			throw new UpdateFailedException();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see net.groupme.model.Style#setBackgroundImgae(java.lang.String)
	 */
	public void setBackgroundImage(String image) throws UpdateFailedException {
		SqlUpdate su = new SqlUpdate(
				ds,
				"UPDATE " + TABLE_SIMPLEGROUPSTYLE + " SET " + SIMPLEGROUPSTYLE_BACKGROUNDIMAGE + " = ? WHERE " + SIMPLEGROUPSTYLE_ID + " = ?"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
		su.declareParameter(new SqlParameter("image", Types.VARCHAR)); //$NON-NLS-1$
		su.declareParameter(new SqlParameter("ID", Types.INTEGER)); //$NON-NLS-1$
		su.compile();
		Object[] oa = new Object[2];
		oa[0] = image;
		oa[1] = new Integer(id);
		int count = su.update(oa);
		if (count == 0)
			throw new UpdateFailedException();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see net.groupme.model.Style#getName()
	 */
	public String getName() {
		GetStringFromTableWhereIDQuery getNameQuery = new GetStringFromTableWhereIDQuery(
				ds, SIMPLEGROUPSTYLE_NAME, TABLE_SIMPLEGROUPSTYLE, id);
		return (String) getNameQuery.execute().iterator().next();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see net.groupme.model.Style#setName(java.lang.String)
	 */
	public void setName(String name) throws UpdateFailedException {
		SqlUpdate su = new SqlUpdate(
				ds,
				"UPDATE " + TABLE_SIMPLEGROUPSTYLE + " SET " + SIMPLEGROUPSTYLE_NAME + " = ? WHERE " + SIMPLEGROUPSTYLE_ID + " = ?"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
		su.declareParameter(new SqlParameter("name", Types.VARCHAR)); //$NON-NLS-1$
		su.declareParameter(new SqlParameter("ID", Types.INTEGER)); //$NON-NLS-1$
		su.compile();
		Object[] oa = new Object[2];
		oa[0] = name;
		oa[1] = new Integer(id);
		int count = su.update(oa);
		if (count == 0)
			throw new UpdateFailedException();
	}

}
